﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using Protocol.SouthGridProtocol.Blocks;
using Protocol.SouthGridProtocol.Decoder;
using Protocol.SouthGridProtocol.Encoder;
using PacoToolKit.Format;
namespace Protocol.SouthGridProtocol.Model
{
    /// <summary>
    /// 读事件记录
    /// </summary>
    public class C0EH
    {
        private static string afn = "0E";
        private static string head = "68";
        private static string bottom = "16";

        private static Seq seq = new Seq(0, 1, 1, 1, 5);
        private static C c = new C(0, 1, 1, null, 0, 11);//prm=1,funcode=11

        private static Hashtable lenHaseTable = new Hashtable {
            //{"E2010001",0},
            {"E2010002",6},
            {"E2010003",6},
            {"E2010004",28},
            {"E2010005",28},
            {"E2010006",28},
            {"E2010007",28},
            {"E2010008",28},
            {"E2010009",28},
            {"E201000A",12},
            {"E201000B",28},
            {"E201000C",28},
            {"E201000D",28},
            {"E201000E",15},
            {"E201000F",28},
            {"E2010010",14},
            {"E2010011",28},
            {"E2010012",28},
            {"E2010013",23},
            //{"E2010014",0},
            {"E2010015",28},
        };


        public static string encodeC0EH(string concentrator, List<C0EHData> dataList)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";

            StringBuilder sb = new StringBuilder();
            foreach (C0EHData data in dataList)
            {
                sb.Append(data.da); sb.Append(data.di); sb.Append(data.beginTime); sb.Append(data.endTime);
            }
            StringBuilder msgsb = new StringBuilder();
            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }

        public static List<CData> decodeC0EH(string msg)
        {
            List<CData> dataList = new List<CData>();
            int msgLength = BaseDecoder.GetLength(msg);//字节数
            int dataRemainLen = (msgLength - 10) * 2;//初始 剩余未读数据长度
            string dataRemain = msg.Substring(32, dataRemainLen);//初始 剩余未读数据

            //循环读取块
            while (dataRemainLen >= 12)
            {
                string da = dataRemain.Substring(0, 4);
                string di = Formater.ReverseHexString(dataRemain.Substring(4, 8));
                int currentDataBlockLength = 0;
                if (di != "E2010001")
                {
                    currentDataBlockLength = (int)lenHaseTable[di];
                }
                else
                {
                    //读取组数
                    //int num = int.Parse(Formater.HEXtoDEC(dataRemain.Substring(12 + 14, 2)));//编程数据标识组数
                    break;//结束读取，返回空
                }
                string data = dataRemain.Substring(12, currentDataBlockLength * 2);

                dataList.Add(new CData { da = da, di = di, data = data });//读完一块，加入列表

                //重新计算剩余未读数据
                dataRemain = dataRemain.Remove(0, (4 + 8 + currentDataBlockLength * 2));
                dataRemainLen = dataRemain.Length;
            }

            return dataList;
        }
    }
}
